package example.service;

import example.dao.AdminUserMapper;
import example.model.AdminUser;
import example.model.AdminUserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * Created by snksnk1091 on 2017/8/23.
 */
@Service
public class AdminUserService {
    @Autowired
    private AdminUserMapper adminUserMapper;

    public void adminUserSignInByAdminUser(AdminUser adminUser){
        adminUserMapper.insertSelective(adminUser);
    }

    public long countAdminUserById(int id){
        AdminUserExample example = new AdminUserExample();
        example.createCriteria().andIdEqualTo(id);
        return adminUserMapper.countByExample(example);
    }

    public long countAdminUserByName(String userName){
        AdminUserExample example = new AdminUserExample();
        example.createCriteria().andUserNameEqualTo(userName);
        return adminUserMapper.countByExample(example);
//        List<AdminUser> list = adminUserMapper.selectByExample(example);
//        if(list == null){
//            return -1;
//        }
//        return list.size();
    }

    public AdminUser getAdminUserById(int id){
        AdminUserExample example = new AdminUserExample();
        example.createCriteria().andIdEqualTo(id);
        List<AdminUser> list = adminUserMapper.selectByExample(example);
        if(list.isEmpty()){
            return null;
        }
        return list.get(0);
    }

    public List<AdminUser> getAdminUserListByUserName(String userName){
        AdminUserExample example = new AdminUserExample();
        example.createCriteria().andUserNameEqualTo(userName);
        return adminUserMapper.selectByExample(example);
    }

    public AdminUser getAdminUserByUserName(String userName){
        //new
        AdminUserExample example = new AdminUserExample();
        example.createCriteria().andUserNameEqualTo(userName);
        List<AdminUser> list = adminUserMapper.selectByExample(example);
        if(list.isEmpty()){
            return null;
        }
        return list.get(0);
    }

    public void updateAdminUser(AdminUser adminUser, int id){
        AdminUserExample example = new AdminUserExample();
        example.createCriteria().andIdEqualTo(id);
        adminUserMapper.updateByExampleSelective(adminUser, example);
    }

    public void deleteAdminUserByUserName(String userName){
        AdminUserExample example = new AdminUserExample();
        example.createCriteria().andUserNameEqualTo(userName);
        adminUserMapper.deleteByExample(example);
    }
}
